local suit = require "/lib/suit-master"
local state = require "lib/stateswitcher"
local moonstate = require "/lib/lovelyMoon"



local unitID = "16";

local testString = "flosama\n{\"Position\":{\"target\":{\"Selected\":["..unitID.."]}}}";

local thread;
local info;

local val = 1;

local gravY = 200;
local gravX = 0;
local sleep = true;

local button1 = "Press me UwU";
local sliderRadius = {value= 25, max = 50};


function love.load()
    
    world = love.physics.newWorld(gravX,gravY,sleep);

    

    thread = love.thread.newThread("thread.lua");
    print("loading")
    thread:start(unitID);
    info = love.thread.getChannel( 'info' ):demand()


    ball = {}
        ball.b = love.physics.newBody(world,400,200,"dynamic");
        ball.b:setMass(40);
        ball.s = love.physics.newCircleShape(sliderRadius.value);
        ball.f = love.physics.newFixture(ball.b,ball.s);
        ball.f:setRestitution(0.4);
        ball.f:setUserData("Ball");

    static = {}
        static.b = love.physics.newBody(world,400,400,"static");
        static.s = love.physics.newRectangleShape(200,50);
        static.f = love.physics.newFixture(static.b, static.s);
        static.f:setUserData("Floor");

end

function pushObject()
    if love.keyboard.isDown("d") then
        ball.b:applyForce(1000, 0) 
    elseif love.keyboard.isDown("a") then
        ball.b:applyForce(-1000, 0) 
    end
    if love.keyboard.isDown("w") then
        ball.b:applyForce(0, -5000)
    elseif love.keyboard.isDown("s") then
        ball.b:applyForce(0, 1000)
    end
end

function love.update(dt)
    world:update(dt)
    pushObject();

    suit.layout:reset(200,200);

    if suit.Button(button1,100,100, 100,30).hit then

        button1 = "I <3 relat";
        state.switch("titlescreen");
        love.thread.getChannel('kill'):push(val);
        
    end

    suit.Slider(sliderRadius,100,150,100,30);
    ball.s = love.physics.newCircleShape(sliderRadius.value);
    
    suit.Label(tostring(sliderRadius.value).." circle size",{align = "left"}, 210,150,100,30);
    
    local temp_inf = love.thread.getChannel( 'info' ):pop();

    if (temp_inf ~= nil) then
        info = temp_inf;
    end

   
    

end


function love.draw()
    
    suit.draw();
    --love.graphics.print(info,100,200);

    love.graphics.circle("line",ball.b:getX(),ball.b:getY(),ball.s:getRadius(),20);
    love.graphics.polygon("fill",static.b:getWorldPoints(static.s:getPoints()));

end